package com.briup.estore.Service;

import com.briup.estore.bean.Book;
import com.briup.estore.bean.Collect;
import com.briup.estore.bean.ext.BookExtend;
import com.briup.estore.mapper.BookMapper;
import com.briup.estore.mapper.CollectMapper;
import com.briup.estore.utils.BeanCopyUtils;
import com.briup.estore.vo.BookInfoVO;
import com.briup.estore.vo.BookVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service

public class BookService {
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private CollectMapper collectMapper;

    public List<BookVO> showBooks() {
        List<Book> books = bookMapper.findAll();
        List<BookVO> bookVOS = BeanCopyUtils.copyBeanList(books, BookVO.class);
        return bookVOS;
    }

    public List<BookVO> findByCategory(int categoryId) {
        List<Book> books = bookMapper.findByCategory(categoryId);
        List<BookVO> bookVOS = BeanCopyUtils.copyBeanList(books, BookVO.class);
        return bookVOS;
    }


    public List<BookVO> getBook(String query) {
        List<Book> books = bookMapper.getBook(query);
        List<BookVO> bookVOS = BeanCopyUtils.copyBeanList(books, BookVO.class);
        return bookVOS;
    }

    public BookInfoVO getBookById(Integer bookId, Integer userId) {
        //书籍和信息分类
        BookExtend bookExtend = bookMapper.selectBookById(bookId);
        BookInfoVO bookInfoVO = BeanCopyUtils.copyBean(bookExtend, BookInfoVO.class);

        //收藏
        //  select * from es_collect  where user_id = '' and book_id = ''
        Collect collect = collectMapper.findByUserIdAndBookId(userId, bookId);
        bookInfoVO.setCollect(collect != null); //true/false
        return bookInfoVO;
    }


}
